package com.tjetc.Impl;

import com.tjetc.domain.Vaccination;
import com.tjetc.mapper.VaccinationMapper;
import com.tjetc.service.VaccinationService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class VaccinationServiceImpl implements VaccinationService {
    VaccinationMapper vaccinationMapper= Db.getMapper(VaccinationMapper.class);
    private Result result=new Result();
    @Override
    public Result add(Vaccination vaccination) {
        int insert = vaccinationMapper.insert(vaccination);
        if (insert==1){
            return new Result().setCode(200).setMsg("添加成功");
        }
        return new Result().setCode(500).setMsg("添加失败");
    }

    @Override
    public Result del(int id) {
        int i = vaccinationMapper.deleteByPrimaryKey(id);
        return i==1?new Result().setCode(200).setMsg("删除成功"):new Result().setCode(500).setMsg("删除失败");
    }

    @Override
    public Result upd(Vaccination vaccination) {
        int i = vaccinationMapper.updateByPrimaryKeySelective(vaccination);
        if(i==1){
            return new Result().setCode(200).setMsg("更新成功");
        }
        return new Result().setCode(500).setMsg("更新失败");
    }


    @Override
    public Vaccination sel(int id) {
        Vaccination vaccination = vaccinationMapper.selectByPrimaryKey(id);
        return vaccination;
    }

    @Override
    public Result set(Vaccination vaccination) {
        try {
            List<Vaccination> list=vaccinationMapper.findBNIYI(vaccination.getNewbornId(),vaccination.getYimiaoId());
            if (!list.isEmpty()){
                return result.setCode(500).setMsg("已为该新生儿添加该种疫苗");
            }else {
                int rows2=vaccinationMapper.set(vaccination);
                return rows2==1?result.setCode(200).setMsg("添加成功"):result.setCode(500).setMsg("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("添加失败");
    }
}
